package com.spica.spicaschool.ui.group

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.drakeet.multitype.ItemViewDelegate
import com.spica.spicaschool.R
import com.spica.spicaschool.bean.group.Group
import com.spica.spicaschool.databinding.ItemGroupBinding
import com.spica.spicaschool.utils.toUid

/**
 * 小组的Item适配器类
 */
class GroupDelegate(val activity: Activity) : ItemViewDelegate<Group, GroupDelegate.ViewHolder>() {


    private val navController =
        Navigation.findNavController(activity, R.id.fragment_container_view)

    class ViewHolder(val viewBinding: ItemGroupBinding) : RecyclerView.ViewHolder(viewBinding.root)

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, item: Group) {
        with(holder.viewBinding) {
            title.text = item.teamName
            tvDesc.text = item.teamDesc
            ivAvatar.load(item.actAvatar)
            tvState.text = "${item.actUserNum}人参与 · ${item.teamLocation}"
            root.setOnClickListener {
                val bundle = bundleOf(Pair("Id", item.teamId))
                navController.navigate(R.id.action_home_to_group_detail, bundle)
            }
        }
    }


    override fun getItemId(item: Group): Long = item.teamUid.toUid().toLong()

    override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder {
        val viewBinding = ItemGroupBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(viewBinding)
    }
}